package test;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class IsAnagramCommand {

	public static void main(String[] args) {
		
		String [] testStrings = new String[] {"Hello", "Hello", "cAt", "ACT", "aboard", "abroad", "Debit card", "bad credit"};
		for(int i = 0; i < testStrings.length; i++) {
			if(i % 2 != 0) {
				System.out.println(execute(testStrings[i-1], testStrings[i]));
			}
		}
		
	}
	
	public static boolean execute(String src, String target) {
		
		if(src.equalsIgnoreCase(target)) {
			return false;
		}
		
		Map<Character, Integer> charNumOfOccurancesMapSrc = createCharNumOfOccurancesMap(src.toLowerCase());
		Map<Character, Integer> charNumOfOccurancesMapTarget = createCharNumOfOccurancesMap(target.toLowerCase());
		
		printMap(charNumOfOccurancesMapSrc);
		printMap(charNumOfOccurancesMapTarget);
		
		return charNumOfOccurancesMapSrc.equals(charNumOfOccurancesMapTarget);
	}

	private static Map<Character, Integer> createCharNumOfOccurancesMap(String src) {
		
		Map<Character, Integer> charNumOfOccurancesMapSrc = new HashMap<>();
		for(Character character : src.toCharArray()) {
			Integer currentNumOfOccurances = charNumOfOccurancesMapSrc.get(character);
			if(currentNumOfOccurances != null) {
				charNumOfOccurancesMapSrc.put(character, ++currentNumOfOccurances);
			} else {
				charNumOfOccurancesMapSrc.put(character, 1);
			}
		}
		return charNumOfOccurancesMapSrc;
	}
	
	private static void printMap(Map<Character, Integer> mapToPrint) {
		
		for(Entry<Character, Integer> entry : mapToPrint.entrySet()) {
			System.out.println(entry.getKey() + "--" + entry.getValue());
		}
		System.out.println("--------------------------------------------------------------------------------");
	}
}
